package com.sign2024.sign2024project.hetong.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.CommonResult;
import com.sign2024.sign2024project.hetong.service.SignMessageService;
import com.sign2024.sign2024project.hetong.vo.SignResultVo;
import com.sign2024.sign2024project.signBase.entry.BaseDisclose;
import com.sign2024.sign2024project.signBase.param.BaseDiscloseParam;
import com.sign2024.sign2024project.signBase.service.BaseDiscloseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author yujie
 * @data 2024/8/16 11:35
 */

@Api(tags = "合同测试")
@RestController
@RequestMapping("/test")
public class TestController {
    @Resource
    private SignMessageService signMessageService;

    @Resource
    private BaseDiscloseService baseDiscloseService;

    @ApiOperation(value = "test", notes = "test")
    @Transactional
    @PostMapping("/save")
    public CommonResult save() throws Exception {
        signMessageService.testInport();
        return CommonResult.success();
    }

    // 测试不创建对象读
    @ApiOperation(value = "导入去重", notes = "导入去重")
    @Transactional
    @PostMapping("/import")
    public void importInfo() throws Exception {
        String filePath = "C:\\Users\\86182\\Desktop\\合同\\中标结果表.xls";
        InputStream inputStream = new FileInputStream(filePath);
        List<SignResultVo> signList =
                EasyExcel.read(inputStream).head(SignResultVo.class).sheet().headRowNumber(2).doReadSync();

        signList.forEach(System.out::println);
        Set<SignResultVo> distinctList = new HashSet<>();
        ArrayList<SignResultVo> vos = new ArrayList<>();
        for (SignResultVo vo : signList) {
            // 判断 数据 是否在 set 中 存在
            if (distinctList.contains(vo)) {
                continue;
            }
            // 添加 数据
            distinctList.add(vo);
            vos.add(vo);
        }
        System.out.println(vos);

        // 将数据 写入 到 一个新的 excel 文件中
        String fileName = "C:\\Users\\86182\\Desktop\\合同\\" + "中标结果表-新.xls";
        EasyExcel.write(fileName, SignResultVo.class).sheet("sheet1")
                .doWrite(vos);
    }

    @GetMapping("/info")
    public CommonResult info(Integer id) {
        System.out.println(id);
        List<String> list = null;
        if (id.equals(1)) {
            list = baseDiscloseService.selectNameList();
        }
        return CommonResult.success(list);
    }

    @ApiOperation(value = "有参数Get", notes = "有参数Get")
    @GetMapping("/page")
    public CommonResult<BaseDisclose> page(BaseDiscloseParam param) {
        Page<BaseDisclose> page = baseDiscloseService.pageBaseDisclose(param);
        return CommonResult.success(page);
    }
}
